package com.eightbears.beboy.ui.user.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.eightbears.beboy.R;
import com.eightbears.beboy.base.BaseActivity;
import com.eightbears.beboy.base.util.CommonUtils;
import com.eightbears.beboy.base.util.ErrorUtils;
import com.eightbears.beboy.base.util.GsonUtil;
import com.eightbears.beboy.base.util.SPUtil;
import com.eightbears.beboy.base.util.okhttp.OkHttpUtils;
import com.eightbears.beboy.base.util.okhttp.callback.StringCallback;
import com.eightbears.beboy.base.view.SwipeBackLayout;
import com.eightbears.beboy.base.view.dialog.CodeDialog;
import com.eightbears.beboy.base.view.qrcode.utils.CodeUtils;
import com.eightbears.beboy.entity.OrderEntity;
import com.eightbears.beboy.entity.PayOrderEntity;
import com.eightbears.beboy.entity.status.Status_Error;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.eightbears.beboy.base.util.CommonAPI.CONNECTION_STATUS_SUCCESS;
import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER;
import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER_ACCEPT;
import static com.eightbears.beboy.base.util.CommonAPI.KEY_HTTP_HEADER_ACCEPT_VALUE;
import static com.eightbears.beboy.base.util.CommonAPI.STATUS_ORDER_USE;
import static com.eightbears.beboy.base.util.CommonAPI.STATUS_UNUSE;
import static com.eightbears.beboy.base.util.CommonAPI.URL_GETSHOPMAELRCODE;
import static com.eightbears.beboy.ui.user.activity.OrderDetailActivity.PARAMS_PAY_ORDER;

public class ShowSetMealInfoActivity extends BaseActivity {

    @BindView(R.id.sw_finish)
    SwipeBackLayout sw_finish;
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.tv_goods)
    TextView tv_goods;
    @BindView(R.id.iv_goods_cover)
    ImageView iv_goods_cover;
    @BindView(R.id.tv_discount_price)
    TextView tv_discount_price;
    @BindView(R.id.tv_cost_price)
    TextView tv_cost_price;
    @BindView(R.id.tv_count)
    TextView tv_count;
    @BindView(R.id.tv_date)
    TextView tv_date;
    @BindView(R.id.tv_event)
    TextView tv_event;

    private PayOrderEntity.PayOrder payOrder;
    private OrderEntity.OrderDetailEntity orderInfo;
    private  CodeDialog.Builder builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_set_meal_info);

        initView();
        initData();
        initEvent();

    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        payOrder = (PayOrderEntity.PayOrder) getIntent().getExtras().get(PARAMS_PAY_ORDER);

    }

    @Override
    protected void initData() {
        if (payOrder != null) {
            getSetMealInfo();

        }
    }

    @Override
    protected void initEvent() {
        sw_finish.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
    }

    @OnClick(R.id.ll_back)
    void back() {
        finish();
    }

    public void getSetMealInfo() {
        String url = URL_GETSHOPMAELRCODE + payOrder.getObject().getCode();
        OkHttpUtils.get().url(url)
                .addHeader(KEY_HTTP_HEADER, "Bearer " + SPUtil.getAccessToken())
                .addHeader(KEY_HTTP_HEADER_ACCEPT, KEY_HTTP_HEADER_ACCEPT_VALUE)
                .build().execute(new StringCallback() {

            @Override
            public void onResponse(String response) {
                orderInfo = GsonUtil.fromJson(response, OrderEntity.OrderDetailEntity.class);
                if (CONNECTION_STATUS_SUCCESS.equals(orderInfo.getStatus_code())) {
                    initPayOrderInfo();
                }
            }

            @Override
            public void onError(Call call, Exception e) {
                Status_Error status_error = GsonUtil.fromJson(e.getMessage(), Status_Error.class);
                ErrorUtils.alertError(ShowSetMealInfoActivity.this, status_error);
            }
        });
    }

    private void initPayOrderInfo() {

        if (orderInfo.getData().getStatus().equals(STATUS_UNUSE)) {
            tv_event.setVisibility(View.VISIBLE);
            tv_event.setText(R.string.text_use);
            tv_event.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String code = "beboy://code/" + payOrder.getObject().getCode();
                    if(null == builder) {
                        builder = new CodeDialog.Builder(ShowSetMealInfoActivity.this);
                        builder.setBitmap(CodeUtils.createImage(code, 150, 150, null));
                        builder.setTitle(orderInfo.getData().getTitle());
                        builder.setTv_code(payOrder.getObject().getCode());
                    }
                    builder.create().show();
                }
            });
        }

        if (orderInfo.getData().getStatus().equals(STATUS_ORDER_USE)) {

        }

        tv_title.setText(orderInfo.getData().getTitle());
        CommonUtils.loadImageView(this, orderInfo.getData().getCover(),
                R.mipmap.icon_default_shop, iv_goods_cover);
        tv_goods.setText(orderInfo.getData().getContent());
        String disPrice = String.format(getString(R.string.text_price),orderInfo.getData().getAmount());
        String price = String.format(getString(R.string.text_price),orderInfo.getData().getPrice());
        String validDate = String.format(
                getString(R.string.text_valid_date),
                CommonUtils.TimeStamp2Date(orderInfo.getData().getStart_at(), "yyyy-MM-dd"),
                CommonUtils.TimeStamp2Date(orderInfo.getData().getEnd_at(), "yyyy-MM-dd"));
        tv_discount_price.setText(disPrice);
        tv_cost_price.setText(price);
        tv_count.setText(orderInfo.getData().getRec_num());
        tv_date.setText(validDate);

    }
}
